<template>
	<view class="detail" :style="{'--color':vuex_config.themeColor1}">
		<template v-if="dataForm.pk_id?true:false">

			<sww-swipe :list='swipeList'></sww-swipe>
			<view class="content">
				<view class="name u-line-2">{{playerForm.name}}</view>
				<view class="title">简介</view>
				<view class="parse_box">
					<u-parse :html="playerForm.details || ''"></u-parse>
				</view>
			</view>
		</template>
		<sww-login></sww-login>
		<view class="fix_bot_main">
			<view class="fixed_bottom_btn">
				<view @click="onShowVote" :class="['word',prodState.type != 1?'disabled':'']">
					{{btnName[prodState.type]}}
				</view>
			</view>
		</view>
		<voting-window ref='ViteRef' v-model="voteNum" @onSumbitVote='onSumbitVote' :dataForm="dataForm" />

	</view>
</template>

<script>
	import {
		downTime
	} from '../components/downTime.js'
	
	import {
		pageShare
	} from '@/mixins/pageShare.js';
	import {
		common
	} from '../components/common.js'
	import votingWindow from '../components/votingWindow'
	export default {
		mixins: [downTime, common,pageShare],
		components: {
			votingWindow
		},
		data() {
			return {
				btnName: {
					0: '活动未开始',
					1: '立即投票',
					2: '活动已结束'
				},
				voteNum: 1,
				swipeList: [],
				dataForm: {},
				playerForm: {},
				id: null,
				activityId: null
			}
		},
		onLoad(options) {
			this.id = Number(options.id)
			this.activityId = Number(options.activityId)
			this.$login('getDetail', null, 'getDetail').then(() => {
				this.getDetail()
			})
		},
		onUnload() {
			if (this.time) {
				clearTimeout(this.time);
			}
		},
		onShareAppMessage: function() {
			return this.getShareAppMessage();
		},
		onShareTimeline: function() {
			return this.getShareTimeline();
		},
		methods: {
			getDetail() {
				this.$loading()
				this.$resPost('GetVotePlayerDetails', {
					pk_id: this.activityId,
					player_id: this.id,
				}).then(({
					data
				}) => {
					let swipeList = []
					if (data.info_player[0].img) {
						data.info_player[0].img.split(',').forEach(item => {
							swipeList.push({
								img: item
							});
						});
					}
					this.swipeList = [].concat(swipeList)
					this.playerForm = {
						...data.info_player[0]
					}
					let useSendNum = this.getUseNum(data)
					this.dataForm = {
						...data,
						useSendNum,
						voted_count: data.voted_count || 0,
						voted_num: data.voted_num || 0,
						info_player: null
					}
					this.$hide()
					this.getStatus(data.start_date, data.end_date)
				})
			},
			downFinish() {
				this.getDetail()
			},

			onShowVote() {
				if(this.prodState.type == 2){
					return this.$toast('活动已结束')
				}
				this.voteNum = 1
				this.$refs.ViteRef.onVoteShow()
			},

			onSumbitVote() {
				this.startSub()
			},
			// 投票
			startSub() {
				let voteNum = Number(this.voteNum)
				this.$login('getDetail').then(() => {
					// 是否在活动时间内
					if (this.prodState.type == 1) {
						if (this.dataForm.vote_every_day == 1) {
							if (this.dataForm.useSendNum) {
								uni.showModal({
									content: `确定投他${voteNum}票？`,
									success: (res) => {
										if (res.confirm) {
											this.$loading()
											this.$resPost('AddUserVoting', {
												pk_id: this.dataForm.pk_id,
												player_id: this.playerForm.pk_id,
												num: voteNum
											}).then(() => {
												this.dataForm.useSendNum -= voteNum
												this.dataForm.voted_num += voteNum
												this.dataForm.voted_count += voteNum
												this.$toast('投票成功')
												this.$hide()
											})
										}
									}
								})
							} else {
								this.$toast('今天的次数已用完')
							}
						} else {
							if (this.dataForm.useSendNum) {
								uni.showModal({
									content: `确定投他${voteNum}票？`,
									success: (res) => {
										if (res.confirm) {
											this.$loading()
											this.$resPost('AddUserVoting', {
												pk_id: this.dataForm.pk_id,
												player_id: this.playerForm.pk_id,
												num: voteNum
											}).then(() => {
												this.dataForm.voted_count += voteNum
												this.dataForm.useSendNum -= voteNum
												this.$toast('投票成功')
												this.$hide()
											})
										}
									}
								})

							} else {
								this.$toast('投票次数已用完')

							}
						}
					} else if (this.prodState.type == 2) {
						this.$toast('活动已结束')
					} else {
						this.$toast('活动未开始')
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.detail {
		.content {
			padding: 0 24rpx;

			.name {
				width: 100%;
				padding: 30rpx 0;
			}

			.title {
				line-height: 40rpx;
				padding: 0 20rpx;
				border-left: 3px solid #31ACF2;
			}
		}

	}

	.glo_button.disabled {
		background-color: #999;
	}

	.parse_box {
		padding: 20rpx;
	}
</style>
